from game import zz_game
from zz import zz_log, zz_img, zz_code
import uiautomator2 as u2

log = zz_log.of()


class Game(zz_game.ZZ_Game):
    def __init__(self, d):
        super().__init__(d)

    def get_name(self):
        return "九梦仙域"

    def game_alert(self):
        self.alert_close_by_file("./game/img/九梦仙域")

    def handle_main(self):
        self.log_msg("等待进入游戏")
        if not self.img_exit("./game/img/九梦仙域/game_start_main.png", 30):
            return False
        self.log_msg("已进入游戏，点击开始")
        self.img_click("./game/img/九梦仙域/game_start.png")
        self.log_msg("等待进入游戏")
        if not self.img_exit("./game/img/九梦仙域/game_main.png", 30):
            return False
        self.log_msg("已进入游戏")
        self.game_alert()

        while not self.check_done():
            for i in range(10):
                self.img_click_if_exists(f'{i}-点击闯关', "./game/img/九梦仙域/play.png", "./game/img/九梦仙域/play_01.png")
                self.log_msg(f"{i}-点击挑战")
                self.img_click("./game/img/九梦仙域/play_01.png")
                for j in range(10):
                    if self.img_exit("./game/img/九梦仙域/play_done.png", 2):
                        break
                    self.d.sleep(3)
                self.log_msg(f"{i}-点击关闭")
                self.img_click("./game/img/九梦仙域/play_close.png")

        return True
